package com.ruoyi.audit.utils;

import com.ruoyi.common.utils.StringUtils;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;

import java.math.BigDecimal;
import java.util.Collection;

public class ActivitiUtils {
    public static BigDecimal getDiscountAmount(BpmnModel bpmnModel, String definitionKey) {
        Process process = bpmnModel.getProcessById(definitionKey);
        Collection<FlowElement> elements = process.getFlowElements();
        for (FlowElement element : elements) {
            if (element instanceof SequenceFlow) {
                SequenceFlow sequenceFlow = (SequenceFlow) element;
                String conditionExpression = sequenceFlow.getConditionExpression();
                if (StringUtils.isNotEmpty(conditionExpression) && conditionExpression.startsWith("${discountAmount")) {
                    String[] split = conditionExpression.split(">=");
                    return new BigDecimal(split[split.length-1].replace("}",""));

                }

            }
        }
        return null;
    }
}
